package com.gaga.console.core.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.gaga.common.enums.AccountAnalysisType;
import com.gaga.common.enums.AccountRecordType;
import com.gaga.console.base.service.impl.SuperServiceImpl;
import com.gaga.console.core.entity.AccountRecord;
import com.gaga.console.core.entity.dto.AccountAnalysisSumByCategoryResultDto;
import com.gaga.console.core.entity.dto.AccountAnalysisSumDto;
import com.gaga.console.core.entity.dto.AccountAnalysisSumResultDto;
import com.gaga.console.core.entity.vo.AccountAnalysisSumByCategoryVo;
import com.gaga.console.core.entity.vo.AccountAnalysisSumResultVo;
import com.gaga.console.core.entity.vo.AccountAnalysisSumVo;
import com.gaga.console.core.mapper.AccountRecordMapper;
import com.gaga.console.core.service.AccountAnalysisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author liujia
 * @since 2023-12-06
 */
@Service
public class AccountAnalysisServiceImpl extends SuperServiceImpl<AccountRecordMapper, AccountRecord> implements AccountAnalysisService {

    @Override
    public List<AccountAnalysisSumResultVo> sumAnalysis(AccountAnalysisSumVo entity) {


        //设置dto
        AccountAnalysisSumDto dto = new AccountAnalysisSumDto();
        dto.setStartDate(entity.getValue() + "-01-01 00:00:00");
        dto.setEndDate(entity.getValue() + "-12-31 23:59:59");
        if (entity.getType() == AccountAnalysisType.day.getCode()) {
            dto.setStartDate(entity.getValue() + "-01 00:00:00");
            dto.setEndDate(DateUtil.format(DateUtil.endOfMonth(DateUtil.parseDate(entity.getValue() + "-01")), DatePattern.NORM_DATE_PATTERN) + " 23:59:59");
        }

        dto.setCreateBy(Integer.valueOf(StpUtil.getLoginId().toString()));
        dto.setType(entity.getType());
        List<AccountAnalysisSumResultDto> resultList = this.baseMapper.sumAnalysis(dto);
        //开始处理数据
        List<AccountAnalysisSumResultVo> reusltVoList = new ArrayList<>();
        if (entity.getType() == AccountAnalysisType.day.getCode()) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtil.parseDate(entity.getValue() + "-01"));
            int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

            for (int i = 1; i <= days; i++) {
                AccountAnalysisSumResultVo vo = new AccountAnalysisSumResultVo();
                //找到相同月份的数据信息
                String date = entity.getValue() + "-" + StringUtils.leftPad(String.valueOf(i), 2, '0');
                List<AccountAnalysisSumResultDto> findResultDataList = resultList
                        .stream()
                        .filter((item) -> item.getDate().equals(date))
                        .collect(Collectors.toList());
                vo.setDate(date);
                if (CollectionUtil.isNotEmpty(findResultDataList)) {
                    Long sumCount = 0L;
                    BigDecimal sumIn = BigDecimal.ZERO;
                    BigDecimal sumOut = BigDecimal.ZERO;
                    for (AccountAnalysisSumResultDto item : findResultDataList) {
                        sumCount += item.getSumCount();
                        if (item.getAccountType() == AccountRecordType.in.getCode()) {
                            sumIn = sumIn.add(item.getSumMoney());
                        } else if (item.getAccountType() == AccountRecordType.out.getCode()) {
                            sumOut = sumOut.add(item.getSumMoney());
                        }
                    }
                    vo.setSumCount(sumCount);
                    vo.setSumIn(sumIn);
                    vo.setSumOut(sumOut);
                    vo.setSumMoney(sumIn.add(sumOut));
                }
                reusltVoList.add(vo);
            }
        } else if (entity.getType() == AccountAnalysisType.month.getCode()) {
            for (int i = 12; i > 0; i--) {
                AccountAnalysisSumResultVo vo = new AccountAnalysisSumResultVo();

                //找到相同月份的数据信息
                String date = entity.getValue() + "-" + StringUtils.leftPad(String.valueOf(i), 2, '0');
                List<AccountAnalysisSumResultDto> findResultDataList = resultList
                        .stream()
                        .filter((item) -> item.getDate().equals(date))
                        .collect(Collectors.toList());
                vo.setDate(date);
                if (CollectionUtil.isNotEmpty(findResultDataList)) {
                    Long sumCount = 0L;
                    BigDecimal sumIn = BigDecimal.ZERO;
                    BigDecimal sumOut = BigDecimal.ZERO;
                    for (AccountAnalysisSumResultDto item : findResultDataList) {
                        sumCount += item.getSumCount();
                        if (item.getAccountType() == AccountRecordType.in.getCode()) {
                            sumIn = sumIn.add(item.getSumMoney());
                        } else if (item.getAccountType() == AccountRecordType.out.getCode()) {
                            sumOut = sumOut.add(item.getSumMoney());
                        }
                    }
                    vo.setSumCount(sumCount);
                    vo.setSumIn(sumIn);
                    vo.setSumOut(sumOut);
                    vo.setSumMoney(sumIn.add(sumOut));
                }
                reusltVoList.add(vo);
            }
        } else if (entity.getType() == AccountAnalysisType.year.getCode()) {
            AccountAnalysisSumResultVo vo = new AccountAnalysisSumResultVo();
            Long sumCount = 0L;
            BigDecimal sumIn = BigDecimal.ZERO;
            BigDecimal sumOut = BigDecimal.ZERO;

            for (AccountAnalysisSumResultDto item : resultList) {
                sumCount += item.getSumCount();
                if (item.getAccountType() == AccountRecordType.in.getCode()) {
                    sumIn = sumIn.add(item.getSumMoney());
                } else if (item.getAccountType() == AccountRecordType.out.getCode()) {
                    sumOut = sumOut.add(item.getSumMoney());
                }
            }
            vo.setDate(entity.getValue());
            vo.setSumCount(sumCount);
            vo.setSumIn(sumIn);
            vo.setSumOut(sumOut);
            vo.setSumMoney(sumIn.add(sumOut));
            reusltVoList.add(vo);
        }
        return reusltVoList;
    }

    @Override
    public List<AccountAnalysisSumByCategoryResultDto> sumAnalysisByCategory(AccountAnalysisSumByCategoryVo entity) {

        //设置dto
        AccountAnalysisSumDto dto = new AccountAnalysisSumDto();

        Date date = DateUtil.parseDate(entity.getValue());

        dto.setStartDate(DateUtil.format(DateUtil.beginOfMonth(date), DatePattern.NORM_DATE_PATTERN) + " 00:00:00");
        dto.setEndDate(DateUtil.format(DateUtil.endOfMonth(date), DatePattern.NORM_DATE_PATTERN) + " 23:59:59");

        dto.setCreateBy(Integer.valueOf(StpUtil.getLoginId().toString()));
        dto.setAccountType(entity.getAccountType());
        List<AccountAnalysisSumByCategoryResultDto> resultList = this.baseMapper.sumAnalysisByCategory(dto);

        return resultList;
    }
}
